home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / nihcl-30.lha / nihcl-3.0 / lib / OIOstream.c < prev    next >
C/C++ Source or Header  |  1990-05-19  |  9KB  |  348 lines

  1. /* OIOstream.c -- implementation of stream Object I/O abstract classes
  2.  
  3.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  4.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  5.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  6.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  7.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  8.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  9.  
  10. Author:
  11.     K. E. Gorlen
  12.     Bg. 12A, Rm. 2033
  13.     Computer Systems Laboratory
  14.     Division of Computer Research and Technology
  15.     National Institutes of Health
  16.     Bethesda, Maryland 20892
  17.     Phone: (301) 496-1111
  18.     uucp: uunet!nih-csl!kgorlen
  19.     Internet: kgorlen@alw.nih.gov
  20.     May, 1989
  21.  
  22. Function:
  23.     
  24. Abstract classes OIOistream and OIOostream for stream Object I/O.
  25.  
  26. Modification History:
  27.  
  28. $Log:    OIOstream.c,v $
  29.  * Revision 3.0  90/05/20  00:20:31  kgorlen
  30.  * Release for 1st edition.
  31.  * 
  32. */
  33.  
  34. #include <ctype.h>
  35. #include "OIOstream.h"
  36. #include "OIOTbl.h"
  37.  
  38. static char rcsid[] = "$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/lib/RCS/OIOstream.c,v 3.0 90/05/20 00:20:31 kgorlen Rel $";
  39.  
  40. extern const int NIHCL_RDEOF,NIHCL_RDFAIL,NIHCL_RDSYNERR,NIHCL_STROV;
  41.     
  42. const unsigned OIOostream::MAXCOL = 80;
  43.  
  44. void OIOistream::checkRead() const    // readFrom input error checking 
  45. {
  46.     char context[31];
  47.     if (strm->good()) return;
  48.     if (strm->eof()) setError(NIHCL_RDEOF,DEFAULT);
  49.     strm->clear();
  50.     context[0] = '\0';
  51.     strm->get(context,sizeof(context),-1);
  52.     setError(NIHCL_RDFAIL,DEFAULT,context);
  53. }
  54.  
  55. void OIOistream::syntaxErr(const char* expect, char was) const
  56. /*
  57.     Report readFrom syntax error.
  58. */
  59. {
  60.     char context[31];
  61.     strm->putback(was);
  62.     context[0] = '\0';
  63.     strm->get(context,sizeof(context));
  64.     setError(NIHCL_RDSYNERR,DEFAULT,expect,context);
  65. }
  66.  
  67. OIOin& OIOistream::operator>>(char* t) { *strm >> t; return *this; }
  68. OIOin& OIOistream::operator>>(char& t) { *strm >> t; return *this; }
  69. OIOin& OIOistream::operator>>(unsigned char& t) { *strm >> t; return *this; }
  70. OIOin& OIOistream::operator>>(short& t) { *strm >> t; return *this; }
  71. OIOin& OIOistream::operator>>(unsigned short& t) { *strm >> t; return *this; }
  72. OIOin& OIOistream::operator>>(int& t) { *strm >> t; return *this; }
  73. OIOin& OIOistream::operator>>(unsigned int& t) { *strm >> t; return *this; }
  74. OIOin& OIOistream::operator>>(long& t) { *strm >> t; return *this; }
  75. OIOin& OIOistream::operator>>(unsigned long& t) { *strm >> t; return *this; }
  76. OIOin& OIOistream::operator>>(float& t) { *strm >> t; return *this; }
  77. OIOin& OIOistream::operator>>(double& t) { *strm >> t; return *this; }
  78. OIOin& OIOistream::operator>>(streambuf* t) { *strm >> t; return *this; }
  79.  
  80. int OIOistream::get() { return strm->get(); }
  81.  
  82. OIOin& OIOistream::get(char& c) { strm->get(c); return *this; }
  83.  
  84. OIOin& OIOistream::get(unsigned char& c) { strm->get(c); return *this; }
  85.  
  86. OIOin& OIOistream::get(char* s, unsigned size)
  87. {
  88.     long f = strm->flags();
  89.     *strm >> hex;
  90.     int i;
  91.     while (size--) {
  92.         *strm >> i;
  93.         *s++ = i;
  94.     }
  95.     strm->flags(f);
  96.     return *this;
  97. }
  98.  
  99. OIOin& OIOistream::get(unsigned char* s, unsigned size)
  100. {
  101.     return get((char*)s,size);
  102. }
  103.  
  104. OIOin& OIOistream::get(short* val, unsigned size)
  105. {
  106.     while (size--) *strm >> *val++;
  107.     return *this;
  108. }
  109.  
  110. OIOin& OIOistream::get(unsigned short* val, unsigned size)
  111. {
  112.     while (size--) *strm >> *val++;
  113.     return *this;
  114. }
  115.  
  116. OIOin& OIOistream::get(int* val, unsigned size)
  117. {
  118.     while (size--) *strm >> *val++;
  119.     return *this;
  120. }
  121.  
  122. OIOin& OIOistream::get(unsigned int* val, unsigned size)
  123. {
  124.     while (size--) *strm >> *val++;
  125.     return *this;
  126. }
  127.  
  128. OIOin& OIOistream::get(long* val, unsigned size)
  129. {
  130.     while (size--) *strm >> *val++;
  131.     return *this;
  132. }
  133.  
  134. OIOin& OIOistream::get(unsigned long* val, unsigned size)
  135. {
  136.     while (size--) *strm >> *val++;
  137.     return *this;
  138. }
  139.  
  140. OIOin& OIOistream::get(float* val, unsigned size)
  141. {
  142.     while (size--) *strm >> *val++;
  143.     return *this;
  144. }
  145.  
  146. OIOin& OIOistream::get(double* val,unsigned size)
  147. {
  148.     while (size--) *strm >> *val++;
  149.     return *this;
  150. }
  151.  
  152. OIOin& OIOistream::get(streambuf& b, char delim)
  153. {
  154.     strm->get(b,delim);
  155.     return *this;
  156. }
  157.  
  158. OIOin& OIOistream::getCString(char* s, unsigned maxlen)
  159. // Read characters in C string format from strm into buffer area s.
  160. // maxlen is the length of s, so at most maxlen-1 characters can be
  161. // read due to the byte required for the null terminator.
  162. {
  163.     unsigned char c;
  164.     char* p = s;
  165.     unsigned n = maxlen;
  166.     *strm >> ws;
  167.     strm->get(c);
  168.     checkRead();
  169.     if (c != '"') syntaxErr("\"",c);
  170.     while ((strm->get(c), c != '"')) {
  171.         if (c == '\n') continue;    // skip newlines
  172.         if (n-- == 0) setError(NIHCL_STROV,DEFAULT,maxlen,s,maxlen);
  173.         if (c != '\\') *p++ = c;
  174.         else switch ((strm->get(c), c)) {
  175.             case 'n' : { *p++ = '\n'; break; }
  176.             case 't' : { *p++ = '\t'; break; }
  177.             case 'b' : { *p++ = '\b'; break; }
  178.             case 'r' : { *p++ = '\r'; break; }
  179.             case 'f' : { *p++ = '\f'; break; }
  180.             case '\\' : { *p++ = '\\'; break; }
  181.             case '"' : { *p++ = '"'; break; }
  182.             case '[' : { *p++ = '{'; break; }
  183.             case ']' : { *p++ = '}'; break; }
  184.             case 'x' : {        // hex character code
  185.                 char buf[4];
  186.                 int i;
  187.                 strm->get(buf,3);
  188.                 sscanf(buf,"%2x",&i);
  189.                 *p++ = i;
  190.                 break;
  191.                 }
  192.             default     : {        // decimal character code for backward compatibility
  193.                 int i;
  194.                 strm->putback(c);
  195.                 *strm >> i; *p++ = i; strm->get(c);
  196.                 if (c != '\\') syntaxErr("\\",c);
  197.                 }
  198.             };
  199.     }
  200.     if (n-- == 0) setError(NIHCL_STROV,DEFAULT,maxlen,s,maxlen);
  201.     *p++ = '\0';
  202.     return *this;
  203. }
  204.  
  205. int OIOistream::precision()        { return strm->precision(); }
  206.  
  207. int OIOistream::precision(int p)    { return strm->precision(p); }
  208.  
  209. int OIOistream::width()            { return strm->width(); }
  210.  
  211. int OIOistream::width(int w)        { return strm->width(w); }
  212.  
  213. OIOout& OIOostream::operator<<(const char* t) { *strm << t; return *this; }
  214. OIOout& OIOostream::operator<<(char t) { *strm << t << '\n'; return *this; }
  215. OIOout& OIOostream::operator<<(unsigned char t) { *strm << t << '\n'; return *this; }
  216. OIOout& OIOostream::operator<<(short t) { *strm << t << '\n'; return *this; }
  217. OIOout& OIOostream::operator<<(unsigned short t) { *strm << t << '\n'; return *this; }
  218. OIOout& OIOostream::operator<<(int t) { *strm << t << '\n'; return *this; }
  219. OIOout& OIOostream::operator<<(unsigned t) { *strm << t << '\n'; return *this; }
  220. OIOout& OIOostream::operator<<(long t) { *strm << t << '\n'; return *this; }
  221. OIOout& OIOostream::operator<<(unsigned long t) { *strm << t << '\n'; return *this; }
  222. OIOout& OIOostream::operator<<(float t) { *strm << t << '\n'; return *this; }
  223. OIOout& OIOostream::operator<<(double t) { *strm << t << '\n'; return *this; }
  224.  
  225. OIOout& OIOostream::put(char c) { strm->put(c); return *this; }
  226.  
  227. OIOout& OIOostream::put(const char* s, unsigned size)
  228. {
  229.     long f = strm->flags();
  230.     *strm << hex;
  231.     while (size--) *strm << (int)*s++ << '\n';
  232.     strm->flags(f);
  233.     return *this;
  234. }
  235.  
  236. OIOout& OIOostream::put(const unsigned char* s, unsigned size)
  237. {
  238.     return put((const char*)s,size);
  239. }
  240.  
  241. OIOout& OIOostream::put(const short* val, unsigned size)
  242. {
  243.     while (size--) *strm << *val++ << '\n';
  244.     return *this;
  245. }
  246.  
  247. OIOout& OIOostream::put(const unsigned short* val, unsigned size)
  248. {
  249.     while (size--) *strm << *val++ << '\n';
  250.     return *this;
  251. }
  252.  
  253. OIOout& OIOostream::put(const int* val, unsigned size)
  254. {
  255.     while (size--) *strm << *val++ << '\n';
  256.     return *this;
  257. }
  258.  
  259. OIOout& OIOostream::put(const unsigned int* val, unsigned size)
  260. {
  261.     while (size--) *strm << *val++ << '\n';
  262.     return *this;
  263. }
  264.  
  265. OIOout& OIOostream::put(const long* val, unsigned size)
  266. {
  267.     while (size--) *strm << *val++ << '\n';
  268.     return *this;
  269. }
  270.  
  271. OIOout& OIOostream::put(const unsigned long* val, unsigned size)
  272. {
  273.     while (size--) *strm << *val++ << '\n';
  274.     return *this;
  275. }
  276.  
  277. OIOout& OIOostream::put(const float* val, unsigned size)
  278. {
  279.     while (size--) *strm << *val++ << '\n';
  280.     return *this;
  281. }
  282.  
  283. OIOout& OIOostream::put(const double* val, unsigned size)
  284. {
  285.     while (size--) *strm << *val++ << '\n';
  286.     return *this;
  287. }
  288.  
  289. void OIOostream::putwrap(char c)
  290. {
  291.     if (++col > MAXCOL) {
  292.         *strm << '\n';
  293.         col = 1;
  294.     }
  295.     *strm << c;
  296. }
  297.  
  298. void OIOostream::putwrap(const char* s, unsigned len)
  299. {
  300.     col += len;
  301.     if (col > MAXCOL) {
  302.         *strm << '\n';
  303.         col = len;
  304.     }
  305.     *strm << s;
  306. }
  307.  
  308. OIOout& OIOostream::putCString(const char* s)
  309. {
  310.     register unsigned char c;
  311.     putwrap("\"",1);
  312.     while (c = *s++) {
  313.         char* p = 0;
  314.         switch (c) {        // check for escape sequence
  315.             case '\n' : { p = "\\n"; break; }    // line feed
  316.             case '\t' : { p = "\\t"; break; }    // horizontal tab
  317.             case '\b' : { p = "\\b"; break; }    // backspace
  318.             case '\r' : { p = "\\r"; break; }    // carriage return
  319.             case '\f' : { p = "\\f"; break; }    // form feed
  320.             case '\\' : { p = "\\\\"; break; }
  321.             case '"'  : { p = "\\\""; break; }
  322.             case '{'  : { p = "\\["; break; }
  323.             case '}'  : { p = "\\]"; break; }
  324.         }
  325.         if (p) putwrap(p,2);    // put 2-character escape sequence
  326.         else {
  327.             if (isprint(c)) putwrap(c);    // printable character
  328.             else {        // hex character code
  329.                 char buf[8];
  330.                 sprintf(buf,"\\x%02x",c);
  331.                 putwrap(buf,4);
  332.             }
  333.         }
  334.     }
  335.     putwrap('\"');
  336.     *strm << '\n';
  337.     col = 0;
  338.     return *this;
  339. }
  340.  
  341. int OIOostream::precision()        { return strm->precision(); }
  342.  
  343. int OIOostream::precision(int p)    { return strm->precision(p); }
  344.  
  345. int OIOostream::width()            { return strm->width(); }
  346.  
  347. int OIOostream::width(int w)        { return strm->width(w); }
  348.